﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using DAL;
using BLL;

public partial class admin_Menus_Details : System.Web.UI.Page
{
  private static int maxLevel;
  MenuBLL menuData = new MenuBLL();
  CategoryBLL categoryData = new CategoryBLL();
  ArticleBLL articleData = new ArticleBLL();
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;
      maxLevel = GetLevelConfig();
      LoadTitle();
      LoadMode();
      LoadLevel();
      LoadPosition();

      if (Request.QueryString["Id"] != null)
        Filldata();
    }
  }
  private int GetLevelConfig()
  {
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.XML");
    var xLevel = xElement.Descendants("MaxLevel").Descendants("Menu");
    XElement level = xLevel.First();
    return Convert.ToInt32(level.Value);
  }
  private void LoadTitle()
  {
    if (Request.QueryString["Id"] != null)
    {
      h3Title.InnerText = "Chỉnh sửa Menu";
    }
  }
  private void LoadLevel()
  {
    dropMenuLevel.Items.Clear();
    for (int i = 1; i <= maxLevel; i++)
    {
      dropMenuLevel.Items.Add(new ListItem("menu cấp " + i.ToString(), i.ToString()));
    }
  }
  private void LoadParent()
  {
    List<Menu_> list = menuData.GetByLevel(1);
    if (list.Count == 0)
    {
      Myfunction.ThongBao(lbThongBao, "Hiện tại chưa có danh mục cấp 1 nào ");
      trParent.Visible = false;
      dropMenuLevel.SelectedValue = "1";
      return;
    }
    if (dropMenuLevel.SelectedIndex == 1)
    {
        dropMenuParent.DataSource = menuData.GetByLevel(1);
        dropMenuParent.DataTextField = "Name";
        dropMenuParent.DataValueField = "Id";
        dropMenuParent.DataBind();
    }
    else
    {
        dropMenuParent.Items.Clear();
        LoadMenu();
    }
  }
  private void LoadMode()
  {
    dropMod.Items.Clear();
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
    var xMode = xElement.Descendants("Menus").Descendants("Modes").Descendants("Mode");
    foreach (XElement item in xMode)
    {
      dropMod.Items.Add(new ListItem(item.Element("Name").Value, item.Attribute("Id").Value));
    }
  }
  private void LoadPosition()
  {
    dropPosition.Items.Clear();
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
    var xPosition = xElement.Descendants("Menus").Descendants("Positions").Descendants("Position");
    foreach (XElement item in xPosition)
    {
      dropPosition.Items.Add(new ListItem(item.Element("Name").Value, item.Attribute("Id").Value));
    }
  }
  //private void LoadProdCate()
  //{
  //  dropCategory.Items.Clear();
  //  ShowChildProdCate(0, " - - ");
  //}
  private void LoadMenu()
  {
      dropCategory.Items.Clear();
      ShowChildMenu(-1, " - - ");
  }
  private void LoadCategory()
  {
    dropCategory.Items.Clear();
    ShowChild(0, " - - ");
  }
  private void LoadArticle()
  {
    int id = Convert.ToInt32(dropCategory.SelectedValue);
    List<Article> listArticle = articleData.GetByCategory(id);
    dropArticle.DataSource = listArticle;
    dropArticle.DataTextField = "Title";
    dropArticle.DataValueField = "Id";
    dropArticle.DataBind();
  }
  private void ShowChildMenu(int parentId, string space)
  {
      List<Menu_> listDataSource = menuData.GetByParent(parentId);

      foreach (Menu_ item in listDataSource)
    {
      ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
      dropMenuParent.Items.Add(obj);

      ShowChildMenu(item.Id, space + "- - - ");
    }
  }
  //private void ShowChildProdCate(int parentId, string space)
  //{
  //    List<ProdCate> listDataSource = prodCateData.GetByParent(parentId);

  //    foreach (ProdCate item in listDataSource)
  //    {
  //        ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
  //        dropCategory.Items.Add(obj);

  //        ShowChildProdCate(item.Id, space + "- - - ");
  //    }
  //}

  private void ShowChild(int parentId, string space)
  {
    List<Category> listDataSource = categoryData.GetByParent(parentId);

    foreach (Category item in listDataSource)
    {
      ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
      dropCategory.Items.Add(obj);

      ShowChild(item.Id, space + "- - - ");
    }
  }
  private void Filldata()
  {
    int id = Convert.ToInt32(Request.QueryString["Id"]);
    Menu_ menu = menuData.GetById(id);
    txtName.Text = menu.Name;
    dropTarget.SelectedValue = menu.Target;
    dropMod.SelectedValue = menu.Mode.ToString();
    dropMenuLevel.SelectedValue = menu.Level.ToString();
    if (menu.Level > 1) trParent.Visible = true;
    LoadParent();
    dropMenuParent.SelectedValue = menu.Parent.ToString();

    switch (menu.Mode)
    {
      case 1: //menu trang chu
        {
          trLink.Visible = true;
          txtLink.Text = "Default.aspx";
          break;
        }
      case 2: //menu 1 tin
      case 7:
        {
          trArticle.Visible = true;
          LoadCategory();
          LoadArticle();
          dropArticle.SelectedValue = menu.Link.ToString();
          break;
        }
      case 3: //menu nhieu tin
        {
          trCategory.Visible = true;
          LoadCategory();
          dropCategory.SelectedValue = menu.Link.ToString();
          break;
        }
      case 5: //menu lien he
        {
          trLink.Visible = true;
          txtLink.Text = "Contact.aspx";
          break;
        }
      case 6: //Menu tuy chinh
        {
          trLink.Visible = true;
          txtLink.Text = menu.Link.ToString();
          break;
        }
      default:
        {
          break;
        }
    }
    if (menu.isEnable == true) radioEnable.Checked = true;
    else radioVisible.Checked = true;
  }
  private bool isValidate()
  {
    if (dropMod.SelectedValue == "2" && dropMod.SelectedIndex == -1)
    {
      Myfunction.ThongBao(lbThongBao,"Chọn bài viết cần trỏ đến ");
      return false;
    }
    return true;
  }
  private void ResetForm()
  {
    txtName.Text = "";
    txtLink.Text = "";
    radioEnable.Checked = true;

    trCategory.Visible = false;
    trArticle.Visible = false;
    trLink.Visible = false;
    dropMod.SelectedIndex = 0;
  }
  private void Update()
  {
    int id = Convert.ToInt32(Request.QueryString["Id"]);
    Menu_ menu = menuData.GetById(id);

    menu.Name = txtName.Text;
    menu.Mode = Convert.ToByte(dropMod.SelectedValue);
  
    menu.Level = Convert.ToInt32(dropMenuLevel.SelectedValue);
    bool isSetParent = false;
    if (menu.Position.ToString() != dropPosition.SelectedValue)
        isSetParent = true;
    if (dropMenuLevel.SelectedValue != "1")
    {        
        menu.Parent = Convert.ToInt32(dropMenuParent.SelectedValue);
    }          
    else menu.Parent = -1;
    menu.Position = Convert.ToByte(dropPosition.SelectedValue);
    if(isSetParent)
        menu.Sort = menuData.GetMaxSort(menu.Position, menu.Parent);
    menu.Target = dropTarget.SelectedValue;
    switch (dropMod.SelectedValue)
    {
      case "2": //menu 1 tin
      case "7":
        {
          menu.Link = dropArticle.SelectedValue;
          break;
        }
      case "3": //menu danh muc tin
      case "4": //menu danh muc san phamr
        {
          menu.Link = dropCategory.SelectedValue;
          break;
        }
      default: //menu tuy chinh
        {
          menu.Link = txtLink.Text;
          break;
        }
    }

    menuData.Update(menu);

    Myfunction.ThongBao(lbThongBao, "Menu đã được cập nhật");
  }
  private void Add()
  {
    Menu_ menu = new Menu_();

    menu.Name = txtName.Text;
    menu.Mode = Convert.ToByte(dropMod.SelectedValue);
    menu.Position = Convert.ToByte(dropPosition.SelectedValue);
    menu.Level = Convert.ToInt32(dropMenuLevel.SelectedValue);

    if (dropMenuLevel.SelectedValue != "1")
      menu.Parent = Convert.ToInt32(dropMenuParent.SelectedValue);
    else menu.Parent = -1;

    menu.Sort = menuData.GetMaxSort(menu.Position,menu.Parent);
    menu.Target = dropTarget.SelectedValue;
    switch (dropMod.SelectedValue)
    {
      case "2": //menu 1 tin
      case "7":
        {
          menu.Link = dropArticle.SelectedValue;
          break;
        }
      case "3": //menu danh muc tin
      case "4": //menu danh muc san phamr
        {
          menu.Link = dropCategory.SelectedValue;
          break;
         }
      default: //menu tuy chinh
        {
          menu.Link = txtLink.Text;
          break;
        }
    }

    menu.isEnable = (radioEnable.Checked ? true : false);

    menuData.Add(menu);
    Myfunction.ThongBao(lbThongBao, "Menu đã được cập nhật");
    ResetForm();
  }
  protected void btnOk_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
      return;
    }
    try
    {
      lbThongBao.Text = "";
      if (isValidate())
      {
        if (Request.QueryString["Id"] == null)
        {
          Add();
          ResetForm();
        }
        else
          Update();

        
      }
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  } 
  protected void dropMod_SelectedIndexChanged(object sender, EventArgs e)
  {
      lbThongBao.Text = "";
      switch (dropMod.SelectedValue)
      {
        case "1":
          {
            trCategory.Visible = false;
            trArticle.Visible = false;

            trLink.Visible = true;
            txtLink.Text = "Default.aspx";
            break;
          }
        case "2": //menu 1 bai viet
        case "7":
          {
            trLink.Visible = false;

            trCategory.Visible = true;
            trArticle.Visible = true;
            dropCategory.AutoPostBack = true;
            LoadCategory();
            LoadArticle();
            break;
          }         
        case "3":  //menu danh muc bai viet
          {
            trLink.Visible = false;
            trArticle.Visible = false;

            trCategory.Visible = true;
            LoadCategory();
            break;
          }

        case "5":  //menu lien he
          {
            trCategory.Visible = false;
            trArticle.Visible = false;

            trLink.Visible = true;
            txtLink.Text = "Contact.aspx";
            break;
          }
        case "6": //menu tuy chinh
          {
            trCategory.Visible = false;
            trArticle.Visible = false;
            trLink.Visible = true;
            txtLink.Text = "Nhập link...";
            break;
          }
      }
  }
  protected void dropCategory_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    LoadArticle();
  }
  protected void dropLevel_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    if (dropMenuLevel.SelectedIndex != 0)
    {
      trParent.Visible = true;
      LoadParent();
    }
    else {
      trParent.Visible = false;
    }
  }
  protected void btnBack_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/admin/Menus/List.aspx");
  }
}